//
//  CZWelcomeViewController.swift
//  CZVVeiBo
//
//  Created by smile on 2017/7/31.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
class CZWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = bgImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    // 界面已经显示
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // 设置动画
        setupHeadImageAnim()
    }
    // MARK: - 阻尼动画(弹簧动画)
    private func setupHeadImageAnim(){
        // 更新头像的顶部约束
        headImageView.snp.updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        // 设置动画
        /*
            - 动画时长
            - 延迟时间
            - 阻尼系数 0-1 （阻尼系数约束 弹性效果越好）
            - 其实加速度
         */
        UIView.animate(withDuration: 2, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
           // 使用约束和UIView 配合动画 需要
            // 强行刷新控制器 进行获取frame
            self.view.layoutIfNeeded()
        }) { (_) in
            // 头像阻尼动画完成
            UIView.animate(withDuration: 0.25, animations: { 
                self.messageLabel.alpha = 1
            }, completion: { (_) in
                // 发送通知告知appdelegate切换根控制器
                NotificationCenter.default.post(name: CZSWITCHROOTVCNOTI, object: "CZWelcomeVc")
            })
        }
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        headImageView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(headImageView.snp.bottom).offset(16)
        }
        
        // 通过sd下载网络图片
//        headImageView.sd_setImage(with: URL(string: CZOAuthViewModel.share.userAccountModel?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default"))
        headImageView.cz_setImage(urlString: CZOAuthViewModel.share.userAccountModel?.avatar_large, placeholderImgName: "avatar_default")
        
    }
    
    // MARK: - 懒加载控件
    // 背景图片
    private lazy var bgImageView: UIImageView = UIImageView(imgName: "ad_background")
    // 头像
    private lazy var headImageView: UIImageView = {
        let img = UIImageView(imgName: "avatar_default")
        // 设置圆的
        img.layer.cornerRadius = 90*0.5
        // 超出圆的部分截取掉
        img.clipsToBounds = true
        // 控件边缘颜色
        img.layer.borderColor = CZAPPThemeColor.cgColor
        // 边缘宽度
        img.layer.borderWidth = 2
        return img
    }()
    // 文字
    private lazy var messageLabel: UILabel = {
        let lab = UILabel()
        // 对齐方式
        lab.textAlignment = .center
        // 大小
        lab.font = UIFont.systemFont(ofSize: 18)
        // 文字
        lab.text = "欢迎回来"
        // 透明度
        lab.alpha = 0
        return lab
    }()

}
